home *** CD-ROM | disk | FTP | other *** search
/ Developer CD Series 2000 November: Tool Chest / Dev.CD Nov 00 TC Disk 1.toast / Sample Code / Archive / Networking / Http Server / •Mac_Classes / TBackGroundApp.cp next >
Encoding:
Text File  |  2000-09-28  |  8.9 KB  |  273 lines  |  [TEXT/CWIE]

  1. //    TBackGroundApp.cp -  Macintosh Background Application class object
  2. // 
  3. // Apple Macintosh Developer Technical Support
  4. // Written by:  Vinne Moscaritolo
  5. //
  6. //  Copyright (work in progress)  Apple Computer, Inc All rights reserved.
  7. //
  8. // You may incorporate this sample code into your applications without
  9. // restriction, though the sample code has been provided "AS IS" and the
  10. // responsibility for its operation is 100% yours.  However, what you are
  11. // not permitted to do is to redistribute the source as "DSC Sample Code"
  12. // after having made changes. If you're going to re-distribute the source,
  13. // we require that you make it clear in the source that the code was
  14. // descended from Apple Sample Code, but that you've made changes.
  15. // 
  16.  
  17. #include "TBackGroundApp.h" 
  18. #include "TMacException.h"
  19.  
  20. // Global Functions
  21. pascal OSErr AEDispatcher(AppleEvent* in,  AppleEvent* out, long Command);
  22.  
  23. // ---------------------------------------------------------------------------
  24. //     TBackGroundApp::fgApplication
  25. // ---------------------------------------------------------------------------
  26. //    pointer to BackGround App object
  27. //
  28. TBackGroundApp*  TBackGroundApp::fgApplication; 
  29.  
  30.  
  31. // ---------------------------------------------------------------------------
  32. //     TBackGroundApp
  33. // ---------------------------------------------------------------------------
  34. //    Default Constructor
  35.  
  36. TBackGroundApp::TBackGroundApp()
  37. {
  38.     TBackGroundApp::fgApplication = this;
  39.     
  40.     this->SetState(TBackGroundApp::kSInit);                // set out first state
  41.     fSleepTime =     kSleep_1_Sec;                        // default sleep value (ticks)
  42.  
  43.     this->InitializeMemory();                            // initialize memory
  44.     this->InstallAEHandler();                            // install our AE handler
  45. }
  46.  
  47.  
  48. // ---------------------------------------------------------------------------
  49. //     ~TBackGroundApp
  50. // ---------------------------------------------------------------------------
  51. //    Destructor
  52.  
  53. TBackGroundApp::~TBackGroundApp()
  54. {
  55. }
  56.  
  57. // ---------------------------------------------------------------------------
  58. //     TBackGroundApp::InitializeMemory
  59. // ---------------------------------------------------------------------------
  60. // initialize memory
  61. void TBackGroundApp::InitializeMemory()
  62. {
  63.     const unsigned long kStackIncrementSize =  (24 * 1024);
  64.     
  65.  // Increase the space allocated for the application stack by lowering the heap limit.
  66.     SetApplLimit((Ptr) ((unsigned long) GetApplLimit() - kStackIncrementSize));
  67.        ThrowIfOSErr( MemError() );
  68.     MaxApplZone();
  69. }
  70.  
  71. // ---------------------------------------------------------------------------
  72. //     TBackGroundApp::InitializeToolbox
  73. // ---------------------------------------------------------------------------
  74. // initialize Required mac toolbox
  75. void TBackGroundApp::InitializeToolbox()
  76. {
  77.  
  78. // Intialize sections need to support Notification Manager
  79.     InitGraf((Ptr) &qd.thePort);
  80.  
  81. // _DON'T_ flush disk-inserted or os events or you'll be sorry! 
  82.     ::FlushEvents(everyEvent - diskMask - osMask, 0);
  83. }
  84.  
  85. // ---------------------------------------------------------------------------
  86. //     TBackGroundApp::DoNextEvent
  87. // ---------------------------------------------------------------------------
  88. //     Handle incoming events
  89.  
  90.  
  91. void TBackGroundApp::DoNextEvent()
  92. {
  93.     if (!::WaitNextEvent(everyEvent, &fEventRecord, fSleepTime, 0L))
  94.         fEventRecord.what = nullEvent;            // security issue
  95. }
  96.  
  97. // ---------------------------------------------------------------------------
  98. //     TBackGroundApp::DoEventLoop
  99. // ---------------------------------------------------------------------------
  100. // event loop swith statement functio
  101.  
  102. void TBackGroundApp::DoEventLoop()
  103. {
  104.     this->DoNextEvent();                        // get the next event record
  105.  
  106.     switch (fEventRecord.what)
  107.     {
  108.         case nullEvent:                            // we got a periodic null event
  109.             this->DoIdle();                        // call idle handler
  110.             break;
  111.  
  112.         case kHighLevelEvent:
  113.             this->DoHighLevelEvent();            // handle our high level events (as AEs)
  114.             break;
  115.  
  116.         default:
  117.             break;
  118.     }
  119. };
  120.  
  121. // ---------------------------------------------------------------------------
  122. //     TBackGroundApp::DoIdle
  123. // ---------------------------------------------------------------------------
  124. // Handle idle time
  125.  
  126. void TBackGroundApp::DoIdle()
  127. {
  128. }
  129.  
  130.  
  131. // ---------------------------------------------------------------------------
  132. //     TBackGroundApp::DoHighLevelEvent
  133. // ---------------------------------------------------------------------------
  134. // Handle high level events.
  135.  
  136. void TBackGroundApp::DoHighLevelEvent()
  137. {
  138.     ::AEProcessAppleEvent(&fEventRecord);
  139. }
  140.  
  141.  
  142. // ---------------------------------------------------------------------------
  143. //     TBackGroundApp::Start
  144. // ---------------------------------------------------------------------------
  145. // Start the application and let it run until we change the state to Quit.
  146.  
  147. void TBackGroundApp::Start()
  148. {
  149.     this->SetState(TBackGroundApp::kSRun);            // set TBackGroundApp to run state
  150.  
  151.     while (fState != TBackGroundApp::kSQuit)
  152.         this->DoEventLoop();
  153.     this->Quit();                                    // out from the state, we will quit
  154. }
  155.  
  156. // ---------------------------------------------------------------------------
  157. //     TBackGroundApp::Quit
  158. // ---------------------------------------------------------------------------
  159. // Quit the application, do any possible cleanup.
  160. void TBackGroundApp::Quit()
  161. {
  162. }
  163.  
  164. // ---------------------------------------------------------------------------
  165. //     TBackGroundApp::InstallAEHandler
  166. // ---------------------------------------------------------------------------
  167. // install Apple event handlers
  168. void TBackGroundApp::InstallAEHandler()
  169. {
  170.     
  171.     ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEOpenApplication,
  172.                     NewAEEventHandlerProc(AEDispatcher), kAppOpen, false));
  173.  
  174.     ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEOpenDocuments,
  175.                     NewAEEventHandlerProc(AEDispatcher), kAppOpenDocuments, false));
  176.  
  177.     ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEPrintDocuments,
  178.                     NewAEEventHandlerProc(AEDispatcher), kAppPrint, false));
  179.  
  180.     ThrowIfOSErr( ::AEInstallEventHandler( kCoreEventClass, kAEQuitApplication,
  181.                     NewAEEventHandlerProc(AEDispatcher), kAppQuit, false));
  182.  
  183. }
  184.  
  185. // ---------------------------------------------------------------------------
  186. //     TBackGroundApp::SetState
  187. // ---------------------------------------------------------------------------
  188. //    STATE CHANGE METHODS
  189. void TBackGroundApp::SetState(TBackGroundApp::EState theState)
  190. {
  191.     fState = theState;
  192. }
  193.  
  194. // ---------------------------------------------------------------------------
  195. //     AEDispatcher
  196. // ---------------------------------------------------------------------------
  197. //    AppleEvent Dispatcher Wrapper
  198.  
  199. pascal OSErr AEDispatcher(AppleEvent* in,  AppleEvent* out, long Command)
  200. {
  201.     return TBackGroundApp::fgApplication->DispatchAppleEvents(in, out, Command);        // dispatch event back to the framework
  202. }
  203.  
  204. // ---------------------------------------------------------------------------
  205. //     TBackGroundApp::DispatchAppleEvents
  206. // ---------------------------------------------------------------------------
  207. // Dispatch to the right Handler based on the command in the AE (refCon).
  208.  
  209. OSErr TBackGroundApp::DispatchAppleEvents(AppleEvent* in, AppleEvent* out, long command)
  210. {
  211.     switch (command)
  212.     {
  213.         case kAppQuit:                            // a quit AE
  214.             return HandleQuit(in, out, command);
  215.  
  216.         case kAppOpen:                            // a  new AE
  217.             return HandleOpen(in, out, command);
  218.  
  219.         case kAppOpenDocuments:                    // an open document AE
  220.             return HandleOpenDocuments(in, out, command);
  221.  
  222.         case kAppPrint:                            // a print AE
  223.             return HandlePrint(in, out, command);
  224.  
  225.         default:                                                // hmm, something else then…
  226.             ThrowMsg("Problem: We are dealing with an AE command that we have no handler for");
  227.             break;
  228.     }
  229.     return errAEEventNotHandled;
  230. }
  231.  
  232. // ---------------------------------------------------------------------------
  233. //     TBackGroundApp::HandleQuit
  234. // ---------------------------------------------------------------------------
  235. // Our General Quit Handler. Set state to Quit and return.
  236.  
  237. OSErr TBackGroundApp::HandleQuit(AppleEvent*,AppleEvent*,long)
  238. {
  239.     this->SetState(TBackGroundApp::kSQuit);        // set state to Quit
  240.     return noErr;                                // need to have this due to AEHandler prototype
  241. }
  242.  
  243. // ---------------------------------------------------------------------------
  244. //     TBackGroundApp::HandleOpen
  245. // ---------------------------------------------------------------------------
  246. // handle the open AE
  247.  
  248. OSErr TBackGroundApp::HandleOpen(AppleEvent*,AppleEvent*,long)
  249. {
  250.     return errAEEventNotHandled;                
  251. }
  252.  
  253. // ---------------------------------------------------------------------------
  254. //     TBackGroundApp::HandleOpenDocuments
  255. // ---------------------------------------------------------------------------
  256. // handle the OpenDocuments AE
  257.  
  258. OSErr TBackGroundApp::HandleOpenDocuments(AppleEvent*,AppleEvent*,long)
  259. {
  260.     return errAEEventNotHandled;                
  261. }
  262.  
  263. // ---------------------------------------------------------------------------
  264. //     TBackGroundApp::HandlePrint
  265. // ---------------------------------------------------------------------------
  266. // handle the Print AE
  267.  
  268. OSErr TBackGroundApp::HandlePrint(AppleEvent*,AppleEvent*,long)
  269. {
  270.     return errAEEventNotHandled;                
  271. }
  272.  
  273.